package com.chengshi.shop.controller.admin.member;

import com.chengshi.shop.controller.common.BaseController;
import com.chengshi.shop.model.member.Member;
import com.chengshi.shop.model.member.MemberAccountLog;
import com.chengshi.shop.model.member.MemberCoupon;
import com.chengshi.shop.model.member.MemberRank;
import com.chengshi.shop.model.order.Order;
import com.chengshi.shop.service.member.MemberAssetsService;
import com.chengshi.shop.service.member.MemberService;
import com.chengshi.shop.service.order.OrderService;
import com.chengshi.shop.service.shop.ShopService;
import com.chengshi.shop.util.DateFormatUtil;
import com.chengshi.shop.util.MessageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * 会员信息相关
 *
 * @author xuxinlong
 * @version 2017年10月11日
 */
@RestController
@RequestMapping("/admin/member")
public class MemberController extends BaseController {
    @Resource
    private MemberService memberService;
    @Resource
    private OrderService orderService;
    @Resource
    private MemberAssetsService memberAssetsService;
    @Resource
    private ShopService shopService;


    /**
     * 会员列表页面
     *
     * @return
     */
    @GetMapping(value = "memberList")
    public ModelAndView memberList() {
        return new ModelAndView("admin/member/memberList");
    }

    /**
     * 订单详情页面
     *
     * @return
     */
    @GetMapping(value = "memberDetail")
    public ModelAndView memberDetail(@RequestParam Integer memberId) {
        ModelAndView mav = new ModelAndView("admin/member/memberDetail");
        Member member = memberService.getMemberByMemberId(memberId);
        MemberRank memberRank = memberService.getMemberRankByMemberId(memberId);
        BigDecimal orderTotalMoney = BigDecimal.ZERO;
        List<Order> orderList = orderService.getValidListByMemberId(memberId);
        for (Order order : orderList) {
            orderTotalMoney = orderTotalMoney.add(order.getTotalAmount().add(order.getPostage()));
        }
        mav.addObject("member", member);
        mav.addObject("memberRank", memberRank);
        mav.addObject("orderTotalMoney", orderTotalMoney);
        mav.addObject("orderCount", orderList.size());
        if (orderList.isEmpty()) {
            mav.addObject("lastOrderTime", "暂无订单");
        } else {
            mav.addObject("lastOrderTime", DateFormatUtil.calculateTime(orderList.get(0).getCreateTime()));
        }
        Integer couponNum = memberAssetsService.getCouponNum(memberId);
        mav.addObject("couponNum", couponNum);
        mav.addObject("postage", shopService.getShopConfig("PACKAGE_POSTAGE"));
        return mav;
    }

    /**
     * 获取会员列表
     *
     * @return
     */
    @GetMapping("/getMemberList")
    public PageInfo<Member> getMemberList(HttpServletRequest request) {
        this.startPage();
        HashMap<String, Object> inMap = new HashMap<>();
        inMap.put("name", request.getParameter("name"));
        List<Member> memberList = memberService.getMemberList(inMap);
        return new PageInfo<>(memberList);
    }

    /**
     * 赠送积分页面
     *
     * @return
     */
    @GetMapping("/giveMemberPoints")
    public ModelAndView giveMemberPoints(@RequestParam Integer memberId) {
        ModelAndView mav = new ModelAndView("admin/member/giveMemberPoints");
        mav.addObject("memberId", memberId);
        return mav;
    }

    /**
     * 赠送会员积分
     *
     * @param memberId
     * @param memberPoints
     * @return
     */
    @PostMapping("/saveMemberPoints")
    public HashMap<String, Object> saveMemberPoints(@RequestParam Integer memberId, @RequestParam BigDecimal memberPoints) {
        HashMap<String, Object> retMap = MessageUtils.success();
        try {
            if (memberPoints.compareTo(BigDecimal.ZERO) != 0) {
                memberAssetsService.addMemberPoints(memberId, memberPoints);
            }
        } catch (Exception e) {
            retMap = MessageUtils.error(e.getMessage());
        }
        return retMap;
    }

    /**
     * 充值余额页面
     *
     * @return
     */
    @GetMapping("/giveMemberBalance")
    public ModelAndView giveMemberBalance(@RequestParam Integer memberId) {
        ModelAndView mav = new ModelAndView("admin/member/giveMemberBalance");
        mav.addObject("memberId", memberId);
        return mav;
    }

    /**
     * 充值会员余额
     *
     * @param memberId
     * @param memberBalance
     * @return
     */
    @PostMapping("/saveMemberBalance")
    public HashMap<String, Object> saveMemberBalance(@RequestParam Integer memberId, @RequestParam BigDecimal memberBalance) {
        HashMap<String, Object> retMap = MessageUtils.success();
        try {
            if (memberBalance.compareTo(BigDecimal.ZERO) != 0) {
                memberAssetsService.addMemberBalance(memberId, memberBalance);
            }
        } catch (Exception e) {
            retMap = MessageUtils.error(e.getMessage());
        }
        return retMap;
    }

    /**
     * 会员优惠券页面
     *
     * @param memberId
     * @return
     */
    @GetMapping("/memberCouponList")
    public ModelAndView memberCouponList(@RequestParam Integer memberId) {
        ModelAndView mav = new ModelAndView("admin/member/memberCouponList");
        mav.addObject("memberId", memberId);
        return mav;
    }

    /**
     * 获取会员拥有优惠券列表
     *
     * @param memberId
     * @return
     */
    @GetMapping("/getMemberCouponList")
    public PageInfo<MemberCoupon> getMemberCouponList(@RequestParam Integer memberId) {
        this.startPage();
        List<MemberCoupon> memberCouponList = memberAssetsService.getCouponListByMemberId(memberId);
        return new PageInfo<>(memberCouponList);
    }

    /**
     * 资产记录明细
     *
     * @param memberId
     * @param accountType
     * @return
     */
    @GetMapping("/memberAccountLog")
    public ModelAndView memberAccountLog(@RequestParam Integer memberId, @RequestParam Byte accountType) {
        ModelAndView mav = new ModelAndView("admin/member/memberAccountLog");
        mav.addObject("memberId", memberId);
        mav.addObject("accountType", accountType);
        return mav;
    }

    /**
     * 获取账户记录
     *
     * @param memberId
     * @param accountType
     * @return
     */
    @GetMapping("/getMemberAccountLog")
    public PageInfo<MemberAccountLog> getMemberAccountLog(@RequestParam Integer memberId, @RequestParam Byte accountType) {
        this.startPage();
        List<MemberAccountLog> logList = memberAssetsService.getMemberAccountLog(memberId, accountType);
        return new PageInfo<>(logList);
    }

    /**
     * 设置成为管理员
     * @param memberId
     * @return
     */
    @PostMapping("/setAdmin")
    public HashMap<String, Object> setAdmin(@RequestParam Integer memberId) {
        HashMap<String, Object> retMap = MessageUtils.success();
        try {
            Member member = memberService.getMemberByMemberId(memberId);
            if (member.getIsAdmin()){
                member.setIsAdmin(false);
            } else {
                member.setIsAdmin(true);
            }
            memberService.saveMember(member);
        } catch (Exception e){
            retMap = MessageUtils.error(e.getMessage());
        }
        return retMap;
    }
}
